home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / system / startppp.000 / startppp / Startppp_v0.80 / Time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-15  |  5.2 KB  |  206 lines

  1. /*
  2.  * Startppp
  3.  *
  4.  * Copyright (C) 1995  Matthias Ott 
  5.  *  (msott@cip.informatik.uni-erlangen.de)
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Library General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Library General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Library General Public
  18.  * License along with this program; if not, write to the Free
  19.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "tk.h"
  25. #include <time.h>
  26. #include <unistd.h>
  27. #include <signal.h>
  28. #include <stdlib.h>
  29. #include <fcntl.h>
  30. #include <termios.h>
  31. #include <sys/types.h>
  32. #include <sys/time.h>
  33. #include <sys/wait.h>
  34.  
  35. #include "Time.h"
  36.  
  37. /*
  38.    **    Function name : gettime
  39.    **
  40.    **    Description : returns time (raw)
  41.    **    Input : -
  42.    **    Output :
  43. */
  44. int GetTime(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
  45. {
  46.   if (argc != 1) {
  47.     interp->result = "wrong # args";
  48.     return TCL_ERROR;
  49.   }
  50.   sprintf(interp->result,"%d",(int)time(NULL));
  51.   return TCL_OK;
  52. }
  53.  
  54. /*
  55.    **    Function name : getshowtime
  56.    **
  57.    **    Description : returns time (hh:mm:ss)
  58.    **    Input : -
  59.    **    Output :
  60. */
  61. int GetShowTime(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
  62. {
  63.   time_t mytime;
  64.   struct tm *myfield;
  65.   if (argc != 1) {
  66.     interp->result = "wrong # args";
  67.     return TCL_ERROR;
  68.   }
  69.   mytime=time(NULL);
  70.   myfield=localtime(&mytime);
  71.   sprintf(interp->result,"%2.2d:%2.2d:%2.2d",myfield->tm_hour,myfield->tm_min,myfield->tm_sec);
  72.   return TCL_OK;
  73. }
  74.  
  75. /*
  76.    **    Function name : OnlineTime
  77.    **
  78.    **    Description : returns difftime
  79.    **    Input : <show $time> = formated, <raw $time> = raw
  80.    **    Output :
  81. */
  82. int OnlineShowTime(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
  83. {
  84.   time_t mytime1, mytime2;
  85.   struct tm *myfield;
  86.   if (argc != 3) {
  87.     interp->result = "wrong # args";
  88.     return TCL_ERROR;
  89.   }
  90.   mytime1=time(NULL);
  91.   mytime2=difftime(mytime1,atof(argv[2]));
  92.   myfield=gmtime(&mytime2);
  93.   if ((strcmp(argv[1],"showsec")==0)) {
  94.     sprintf(interp->result,"%2.2d:%2.2d:%2.2d",myfield->tm_hour,myfield->tm_min,myfield->tm_sec);
  95.   } else if ((strcmp(argv[1],"raw")==0)) {
  96.     sprintf(interp->result,"%d",(int)mytime2);
  97.   } else if ((strcmp(argv[1],"showmin")==0)) {
  98.     sprintf(interp->result,"%2.2d:%2.2d",myfield->tm_hour,myfield->tm_min);
  99.   }
  100.   return TCL_OK;
  101. }
  102.  
  103. /*
  104.    **    Function name : Showdifftime
  105.    **
  106.    **    Description : converts rawtime to showtime
  107.    **    Input : <show $time> = formated
  108.    **    Output :
  109. */
  110. int ShowDiffTime(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
  111. {
  112.   time_t mytime1, mytime2;
  113.   struct tm *myfield;
  114.   if (argc != 3) {
  115.     interp->result = "wrong # args";
  116.     return TCL_ERROR;
  117.   }
  118.   mytime1=0;
  119.   mytime2=difftime(mytime1,atof(argv[2]));
  120.   myfield=gmtime(&mytime2);
  121.   if ((strcmp(argv[1],"showsec")==0)) {
  122.     sprintf(interp->result,"%2.2d:%2.2d:%2.2d",myfield->tm_hour,myfield->tm_min,myfield->tm_sec);
  123.   } else if ((strcmp(argv[1],"showmin")==0)) {
  124.     sprintf(interp->result,"%2.2d:%2.2d",myfield->tm_hour,myfield->tm_min);
  125.   }
  126.   return TCL_OK;
  127. }
  128.  
  129. /*
  130.    **    Function name : GetPartZone var start stop zoneday(0-6)
  131.    **
  132.    **    Description : returns zone
  133.    **    Input : <var> : the variable which should be changed
  134.    **              <start, stop> : time for nightzone 
  135.    **    Output : 1 for daytime, 2 for nighttime
  136. */
  137. int GetPartZone(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
  138. {
  139.   time_t mytime;
  140.   int i;
  141.   char zone[]="1";
  142.   char filename[100];
  143.   struct tm *myfield;
  144.   if (argc != 11) {
  145.     interp->result = "wrong # args";
  146.     return TCL_ERROR;
  147.   }
  148.   mytime=time(NULL);
  149.   myfield=localtime(&mytime);
  150.   if (atoi(argv[myfield->tm_wday+4])==1) {
  151.     strcpy(zone,"2");
  152.   }
  153.   else {
  154.     if (atoi(argv[2]) > atoi(argv[3])) {
  155.       if ((myfield->tm_hour>=atoi(argv[2])) || (myfield->tm_hour<atoi(argv[3]))) 
  156.     strcpy(zone,"2");
  157.     }
  158.     else {
  159.       if ((myfield->tm_hour>=atoi(argv[2])) && (myfield->tm_hour<atoi(argv[3]))) 
  160.     strcpy(zone,"2");
  161.     }
  162.   }
  163.   if (zone != "2") {
  164.     FILE *fd;
  165.     int day;
  166.     int nowday;
  167.     nowday = (myfield->tm_mday * 100) + myfield->tm_mon + 1;
  168.     sprintf(filename,"%s/.startppp_holidays",getenv("HOME"));
  169.     fd=fopen(filename,"r");
  170.     if (fd==NULL)
  171.       return TCL_ERROR;
  172.     while ((fscanf(fd,"%d",&day))!=EOF) {
  173.       if (day == nowday) {
  174.     strcpy(zone,"2");
  175.     break;
  176.       }
  177.     }
  178.     fclose (fd);
  179.   }  
  180.   sprintf(interp->result,"%s",zone); 
  181.   Tcl_SetVar (interp, argv[1], zone,TCL_GLOBAL_ONLY);
  182.   return TCL_OK;
  183. }
  184.  
  185. /*
  186.    **    Function name : SetIconName
  187.    **
  188.    **    Description :
  189.    **    Input :
  190.    **    Output :
  191. */
  192. int SetIconName(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
  193. {
  194.   Tk_Window tkwin;
  195.   if (argc != 1) {
  196.     interp->result = "wrong # args";
  197.     return TCL_ERROR;
  198.   }
  199.   tkwin = Tk_MainWindow(interp);
  200.   /*  tkwin = (Tk_Window) clientData; */
  201.   XSetIconName(Tk_Display(tkwin),Tk_WindowId(tkwin),"XlibIcon");
  202.   
  203.   return TCL_OK;
  204. }
  205.  
  206.